<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="mini" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="上级分类" prop="parentName">
        <el-popover
          ref="goodsListPopover"
          placement="bottom-start"
          trigger="click">
          <goods-catalog-tree @select-change="goodsTreeSelectChange"></goods-catalog-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:goodsListPopover :readonly="true" placeholder="点击选择上级分类" class="menu-list__input"></el-input>
      </el-form-item>
      <!-- <el-form-item label="上级分类" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder=""></el-input>
      </el-form-item> -->
    <el-form-item label="服务名称" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input type="textarea" v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import GoodsCatalogTree from './goodsCatalogTree'
  export default {
    components: {
      GoodsCatalogTree
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          parentName: '',
          name: '',
          level: '',
          productCount: '',
          productUnit: '',
          navStatus: '',
          showStatus: '',
          sort: '',
          icon: '',
          keywords: '',
          description: '',
          createTime: '',
          updateTime: '',
          deleteTime: '',
          version: '',
          deleted: ''
        },
        dataRule: {
          parentName: [
            { required: true, message: '上级分类不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '服务名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bid/goodscategory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.goodsCategory.parentId
                this.dataForm.parentName = data.goodsCategory.parentName
                this.dataForm.name = data.goodsCategory.name
                this.dataForm.level = data.goodsCategory.level
                this.dataForm.productCount = data.goodsCategory.productCount
                this.dataForm.productUnit = data.goodsCategory.productUnit
                this.dataForm.navStatus = data.goodsCategory.navStatus
                this.dataForm.showStatus = data.goodsCategory.showStatus
                this.dataForm.sort = data.goodsCategory.sort
                this.dataForm.icon = data.goodsCategory.icon
                this.dataForm.keywords = data.goodsCategory.keywords
                this.dataForm.description = data.goodsCategory.description
                this.dataForm.createTime = data.goodsCategory.createTime
                this.dataForm.updateTime = data.goodsCategory.updateTime
                this.dataForm.deleteTime = data.goodsCategory.deleteTime
                this.dataForm.version = data.goodsCategory.version
                this.dataForm.deleted = data.goodsCategory.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bid/goodscategory/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'parentName': this.dataForm.parentName,
                'name': this.dataForm.name,
                'level': this.dataForm.level,
                'productCount': this.dataForm.productCount,
                'productUnit': this.dataForm.productUnit,
                'navStatus': this.dataForm.navStatus,
                'showStatus': this.dataForm.showStatus,
                'sort': this.dataForm.sort,
                'icon': this.dataForm.icon,
                'keywords': this.dataForm.keywords,
                'description': this.dataForm.description,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'deleteTime': this.dataForm.deleteTime,
                'version': this.dataForm.version,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      goodsTreeSelectChange (data, node) {
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.label
      }
    }
  }
</script>
